<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/11
 * Time: 17:31
 */

namespace app\model;

use think\facade\Db;
use think\Model;

class User extends Model
{
    protected $name = 'user';
    protected $table = 'user';
    protected $pk = 'id';

    const SYS_ADMIN = 0;         //超管
    const CLEANING = 1;         //保洁
    const CLEANING_HEAD = 2;    //领班
    const CLEANING_CHARGE = 3;  //主管
    const CLEANING_BOSS = 4;    //管理员

    protected static function init()
    {
        //TODO:初始化内容
    }

    private function _getOutListField()
    {
        return [
            'id' => 'id',
            'pername' => 'pername',
            'phone' => 'phone',
            'contact' => 'contact',
            'position' => 'position',
            'createtime' => 'createtime'
        ];
    }

    public function addOneUser($data)
    {
        $data['createtime'] = time();
        return Db::table('user')->insertGetId($data);
    }


    public function getUserInfoById($id,$fields = "*")
    {
        return Db::table('user')->where(['id'=>$id])->field($fields)->find();
    }

    public function getUserDetailInfoById($id,$fields = "*")
    {
        return Db::table('user')
            ->leftJoin('organization org','user.orgid=org.id')
            ->where(['user.id'=>$id])
            ->field($fields)
            ->find();
    }

    public function getUserInfoByPhone($phone,$fields = "*")
    {
        return Db::table('user')->where(['phone'=>$phone])->field($fields)->find();
    }

    public function getUserAdminInfoByPhone($phone,$fields = "*")
    {
        $where = [
            ['position','in',[0,3,4]],
            ['phone','=',$phone],
        ];

        return $this->alias('user')->leftJoin('organization org','user.orgid=org.id')->where($where)->field($fields)->find();
    }

    public function getUserAdminInfoByWhere($where,$fields = "*")
    {
        return $this->alias('user')
            ->leftJoin('organization org','user.orgid=org.id')
            ->where($where)
            ->field($fields)
            ->find();
    }

    public function getUserListByCondition($where = '', $order = 'id desc', $field = ''){

        $field = $field ?: self::_getOutListField();
        $videoDb = Db::table('user')
            ->where($where)
            ->field($field)
            ->order($order)
            ->paginate(input('param.pageSize'))
            ->render();

        return $videoDb;
    }

    /*
     * 获取小程序可登录用户的信息
     */
    public function getUserIndexInfoByPhone($phone,$fields = "*")
    {
        $where = " (position like '%1%' || position like '%2%' || position like '%3%') && phone={$phone} && user.status=1 ";
        return $this->alias('user')
            ->leftJoin('organization org','user.orgid=org.id')
            ->where($where)
            ->field($fields)
            ->find();
    }

    public function updateUserInfoByWhere($where,$data)
    {
        $data['updatetime'] = time();
        return Db::table('user')->where($where)->update($data);
    }

    public function modifyUserIntegral($uid,$num){
        $modify = Db::table('user')->where(['id'=>$uid]);
        if($num >= 0){
            $modify->inc('integral',$num);
        }else{
            $modify->dec('integral',$num);
        }
        return $modify->update();
    }
    public function addUserIntegral($uid,$num)
    {
        return Db::table('user')->where(['id'=>$uid])->inc('integral',$num)->update();
    }
    public static function reduceUserIntegral($uid,$num)
    {
        return Db::table('user')->where(['id'=>$uid])->dec('integral',$num)->update();
    }

}